<template>
  <div class="system-management">
    <!-- 当访问子路由时，显示子路由内容 -->
    <router-view v-if="$route.path !== '/system'" />
    
    <!-- 当访问主路由时，显示系统管理首页 -->
    <div v-else>
      <el-row :gutter="20">
        <!-- 系统配置卡片 -->
        <el-col :span="8">
          <el-card class="system-card" @click="navigateToConfig">
            <div class="card-content">
              <div class="card-icon">
                <el-icon size="40" color="#409eff">
                  <Setting />
                </el-icon>
              </div>
              <div class="card-info">
                <h3>系统配置</h3>
                <p>管理系统配置参数，包括站点信息、上传设置等</p>
              </div>
            </div>
          </el-card>
        </el-col>

        <!-- 系统监控卡片 -->
        <el-col :span="8">
          <el-card class="system-card" @click="navigateToMonitor">
            <div class="card-content">
              <div class="card-icon">
                <el-icon size="40" color="#67c23a">
                  <Monitor />
                </el-icon>
              </div>
              <div class="card-info">
                <h3>系统监控</h3>
                <p>实时监控系统资源使用情况，包括CPU、内存、磁盘等</p>
                <el-tag :type="systemStatus.monitorEnabled ? 'success' : 'danger'" size="small" style="margin-top: 8px;">
                  {{ systemStatus.monitorEnabled ? '已启用' : '已禁用' }}
                </el-tag>
              </div>
            </div>
          </el-card>
        </el-col>

        <!-- 系统日志卡片 -->
        <el-col :span="8">
          <el-card class="system-card" @click="navigateToLog">
            <div class="card-content">
              <div class="card-icon">
                <el-icon size="40" color="#e6a23c">
                  <Document />
                </el-icon>
              </div>
              <div class="card-info">
                <h3>系统日志</h3>
                <p>查看系统操作日志，包括用户操作、系统事件等</p>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <el-row :gutter="20" style="margin-top: 20px;">
        <!-- 登录日志卡片 -->
        <el-col :span="8">
          <el-card class="system-card" @click="navigateToLoginLog">
            <div class="card-content">
              <div class="card-icon">
                <el-icon size="40" color="#f56c6c">
                  <User />
                </el-icon>
              </div>
              <div class="card-info">
                <h3>登录日志</h3>
                <p>查看用户登录记录，包括登录时间、IP地址、登录状态等</p>
              </div>
            </div>
          </el-card>
        </el-col>

        <!-- 定时任务卡片 -->
        <el-col :span="8">
          <el-card class="system-card" @click="navigateToJob">
            <div class="card-content">
              <div class="card-icon">
                <el-icon size="40" color="#909399">
                  <Clock />
                </el-icon>
              </div>
              <div class="card-info">
                <h3>定时任务</h3>
                <p>管理系统定时任务，包括任务配置、执行日志等</p>
              </div>
            </div>
          </el-card>
        </el-col>

        <!-- 数据备份卡片 -->
        <el-col :span="8">
          <el-card class="system-card" @click="navigateToBackup">
            <div class="card-content">
              <div class="card-icon">
                <el-icon size="40" color="#9c27b0">
                  <Download />
                </el-icon>
              </div>
              <div class="card-info">
                <h3>数据备份</h3>
                <p>管理系统数据备份，包括备份创建、恢复、下载等</p>
                <el-tag :type="systemStatus.autoBackupEnabled ? 'success' : 'danger'" size="small" style="margin-top: 8px;">
                  {{ systemStatus.autoBackupEnabled ? '自动备份已启用' : '自动备份已禁用' }}
                </el-tag>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 系统概览 -->
      <el-card style="margin-top: 20px;">
        <template #header>
          <div class="card-header">
            <span>系统概览</span>
          </div>
        </template>
        
        <el-row :gutter="20">
          <el-col :span="6">
            <div class="stat-item">
              <div class="stat-number">{{ systemStats.configCount }}</div>
              <div class="stat-label">系统配置</div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="stat-item">
              <div class="stat-number">{{ systemStats.logCount }}</div>
              <div class="stat-label">系统日志</div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="stat-item">
              <div class="stat-number">{{ systemStats.jobCount }}</div>
              <div class="stat-label">定时任务</div>
            </div>
          </el-col>
          <el-col :span="6">
            <div class="stat-item">
              <div class="stat-number">{{ systemStats.backupCount }}</div>
              <div class="stat-label">数据备份</div>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Setting, Monitor, Document, User, Clock, Download } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { getConfigStatus } from '@/api/system'

const router = useRouter()

// 系统统计数据
const systemStats = ref({
  configCount: 0,
  logCount: 0,
  jobCount: 0,
  backupCount: 0
})

// 系统状态
const systemStatus = ref({
  monitorEnabled: true,
  autoBackupEnabled: true
})

// 导航方法
const navigateToConfig = () => {
  router.push('/system/config')
}

const navigateToMonitor = () => {
  router.push('/system/monitor')
}

const navigateToLog = () => {
  router.push('/system/log')
}

const navigateToLoginLog = () => {
  router.push('/system/login-log')
}

const navigateToJob = () => {
  router.push('/system/job')
}

const navigateToBackup = () => {
  router.push('/system/backup')
}

// 加载系统统计数据
const loadSystemStats = () => {
  // 这里可以调用API获取系统统计数据
  systemStats.value = {
    configCount: 25,
    logCount: 1024,
    jobCount: 5,
    backupCount: 3
  }
}

// 检查系统配置状态
const checkSystemStatus = async () => {
  try {
    const response = await getConfigStatus()
    if (response.code === 200) {
      systemStatus.value = {
        monitorEnabled: response.data.monitorEnabled,
        autoBackupEnabled: response.data.autoBackupEnabled
      }
    }
  } catch (error) {
    console.error('获取系统状态失败:', error)
    ElMessage.error('获取系统状态失败')
  }
}

onMounted(() => {
  loadSystemStats()
  checkSystemStatus()
})
</script>

<style scoped>
.system-management {
  padding: 20px;
}

.system-card {
  cursor: pointer;
  transition: all 0.3s;
  height: 200px;
}

.system-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.card-content {
  display: flex;
  align-items: center;
  height: 100%;
}

.card-icon {
  margin-right: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 80px;
  height: 80px;
  background: #f5f7fa;
  border-radius: 50%;
}

.card-info h3 {
  margin: 0 0 10px 0;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.card-info p {
  margin: 0;
  color: #606266;
  font-size: 14px;
  line-height: 1.5;
}

.card-header {
  font-weight: bold;
  color: #303133;
}

.stat-item {
  text-align: center;
  padding: 20px;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 10px;
}

.stat-label {
  color: #606266;
  font-size: 14px;
}
</style>
